<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JS代码捕捉回车键</title>
</head>
<body>
<script type="text/javascript">
  window.onload=function (){
    var usernameElt=document.getElementById("username");
    //回车键的键值为13，esc的键值为27
    usernameElt.onkeydown=function (event){
      //alert("111");
      if (event.keyCode===13){
        alert("用户正在登录");
      }
    }
  }
</script>
<input type="text" id="username">
</body>
</html>